package com.ee.chat.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.admin.service.IOperationService;
import com.ee.chat.admin.service.IVersionService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.SysVersionMapper;
import com.ee.chat.common.entity.SysVersion;
import com.ndhutu.ee.util.DateUtil;
import com.ndhutu.ee.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Service
public class VersionServiceImpl implements IVersionService {

    @Resource
    SysVersionMapper sysVersionMapper;
    @Resource
    IOperationService operationService;

    @Override
    public Result list(HttpServletRequest request, Integer page, Integer limit, Integer type, String time) throws Exception {
        Map<String, Object> data = new HashMap<>();

        if(type == null && StringUtil.isEmpty(time)){
            HttpSession session = request.getSession(true);
            List<String> userPermissions = (List<String>)session.getAttribute("userPermissions");
            if(!userPermissions.contains("/version/viewList")){
                data.put("datas", new ArrayList<>());
                data.put("total", 0);

                return Result.returnSuccess(data);
            }
        }

        Date beginTime = null, endTime = null;
        if(!StringUtil.isEmpty(time)){
            String[] arr = time.split(" - ");
            beginTime = DateUtil.parse(arr[0]);
            endTime = DateUtil.parse(arr[1]);
        }

        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<SysVersion> iPage = sysVersionMapper.list(p, type, beginTime, endTime);

        data.put("datas", iPage.getRecords());
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "查询app版本列表数据");

        return Result.returnSuccess(data);
    }

    @Override
    public Result updateStatus(HttpServletRequest request, Integer id, Integer status) throws Exception {
        SysVersion sysVersion = sysVersionMapper.selectById(id);

        sysVersion.setStatus(status);
        sysVersionMapper.updateById(sysVersion);

        operationService.addOperationLog(request, "变更"+id+"app状态");

        return Result.returnSuccess(null);
    }

    @Override
    public Result save(HttpServletRequest request, SysVersion sysVersion) throws Exception {
        if(sysVersion.getId() == null){
            //添加新版本
            sysVersionMapper.insert(sysVersion);

            operationService.addOperationLog(request, "新增app新版本，id："+sysVersion.getId());
        }else {
            //编辑
            sysVersionMapper.updateById(sysVersion);

            operationService.addOperationLog(request, "编辑app版本，id："+sysVersion.getId());
        }

        return Result.returnSuccess(null);
    }

    @Override
    public Result toEdit(HttpServletRequest request, Integer id) throws Exception {
        Map<String, Object> data = new HashMap<>();

        SysVersion sysVersion = sysVersionMapper.selectById(id);
        sysVersion = sysVersion == null ? new SysVersion() : sysVersion;
        data.put("sysVersion", sysVersion);

        return Result.returnSuccess(data);
    }
}
